A line chart uses points connected by line segments to show information that changes over time.
1. Plot a line chart
1.1 plt.plot
This module contains all the 2D line class which can draw with a variety of line styles, markers and colors.
import matplotlib.pyplot as plt
plt.plot(*args, **kwargs)
# The kwargs are Line2D properties
(xdata, ydata, linewidth=None, linestyle=None, color=None, marker=None, markersize=None, markeredgewidth=None, markeredgecolor=None, markerfacecolor=None, markerfacecoloralt='none', fillstyle=None, antialiased=None, dash_capstyle=None, solid_capstyle=None, dash_joinstyle=None, solid_joinstyle=None, pickradius=5, drawstyle=None, markevery=None, **kwargs)
Valid kwargs are listed on Line2D properties. All of the line properties can be controlled by keyword arguments,
xdata
ydate
linewidth
orlw
, float value in pointslinestyle
orls
'-'
or'solid'
solid line'--'
or'dashed'
dashed line'-.'
or'dash_dot'
dash-dotted line':'
or'dotted'
dotted line'None'
or' '
or''
draw nothing
color
orc
any matplotlib color, refer to plt.colors()- b: blue
- g: green
- r: red
- c: cyan
- m: magenta
- y: yellow
- k: black
- w: white
marker
The modulematplotlib.markers
is used by plot and scatter to handle markers.label
Set the label to s (string or anything printable with ‘%s’ conversion) for auto legend.
1.2 Save to file
savefig(*args, **kwargs)
is used to save the current figure.
plt.savefig(fname, dpi=None, facecolor='w', edgecolor='w',
orientation='portrait', papertype=None, format=None,
transparent=False, bbox_inches=None, pad_inches=0.1,
frameon=None)
1.3 Others
Return a subplot axes positioned by the given grid definition.
Typical call signature:
plt.subplot(nrows, ncols, plot_number)
# for instance
fig, ax = plt.subplots()
Get the current Axes instance on the current figure matching the given keyword args, or create one.
plt.clf()
Clear the current figure.
2. Decoration
(1) Legend
Other parameters, such as loc
, the location of the legend,
'best' : 0, (only implemented for axes legends)
'upper right' : 1,
'upper left' : 2,
'lower left' : 3,
'lower right' : 4,
'right' : 5,
'center left' : 6,
'center right' : 7,
'lower center' : 8,
'upper center' : 9,
'center' : 10,
(2) ticks
plt.xticks(*args, **kwargs)
and plt.yticks(*args, **kwargs)
Get or set the x-limits of the current tick locations and labels.
# set the locations and labels of the xticks/yticks
locs, labels = plt.xticks(arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue'))
locs, labels = plt.yticks(arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue'))
(3) labels
plt.xlabel(s, *args, **kwargs)
and plt.ylabel(s, *args, **kwargs)
is to set the x/y axis label of the current axis. kwargs
is Text properties, listed here.
# plt.xlabel(s, *args, **kwargs)
override = {
'fontsize' : 'small',
'verticalalignment' : 'top',
'horizontalalignment' : 'center'
}
# plt.ylabel(s, *args, **kwargs)
override = {
'fontsize' : 'small',
'verticalalignment' : 'center',
'horizontalalignment' : 'right',
'rotation' : 'vertical'
}
(4) limits of axes
plt.xlim(*args, **kwargs)
and plt.ylim(*args, **kwargs)
get or set the x/y limits of the current axes.
xmin, xmax = plt.xlim() # return the current xlim, a length 2 tuple
xmin, xmax = plt.xlim(xmin, xmax) # OR plt.xlim((xmin, xmax)), set the xlim to xmin, xmax
xmin, xmax = plt.xlim(xmax=3) # adjust the max leaving min unchanged
Decormation